package com.duguo.dynamicmvc.view.forward;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.security.Principal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.duguo.dynamicmvc.model.Model;

public class ForwardRequestWrapper implements HttpServletRequest {
	
	private HttpServletRequest request;
	private Model model;
	private String servletPath;

	public ForwardRequestWrapper(HttpServletRequest request, Model model,String servletPath) {
		this.request = request;
		this.model = model;
		this.servletPath=servletPath;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public Model getModel() {
		return model;
	}

	public String getServletPath() {
		return servletPath;
	}

	public String getParameter(String name) {
		if(model!=null){
			Object value=model.get(name);
			if(value!=null){
				if(value instanceof String)
					return (String)value;
				else if(value.getClass().isArray() && Array.getLength(value)>0)
					return Array.get(value, 0).toString();
				else
					return value.toString();
			}
		}
		return request.getParameter(name);
	}
	

	@SuppressWarnings("unchecked")
	public Map getParameterMap() {
		Map params=new HashMap();
		params.putAll(request.getParameterMap());
		if(model!=null){
			for(Object obj:model.entrySet()){
				Map.Entry entry=(Map.Entry)obj;
				if(entry.getValue().getClass().isArray()){
					String[] array=new String[Array.getLength(entry.getValue())];
					if(array.length>0){
						for(int i=0;i<array.length;i++)
							array[i]=Array.get(entry.getValue(), i).toString();
					}
					params.put(entry.getKey().toString(),array);
				}else{
					params.put(entry.getKey().toString(),entry.getValue().toString());
				}
			}
		}
		return params;
	}

	@SuppressWarnings("unchecked")
	public Enumeration getParameterNames() {
		Set namesSet=new HashSet();

		if(model!=null){
			for(Object key:model.keySet())
				namesSet.add(key.toString());
		}
		Enumeration tempNames=request.getParameterNames();
		while(tempNames.hasMoreElements())
			namesSet.add(tempNames.nextElement());
		
		Vector names=new Vector();
		names.addAll(namesSet);
		return names.elements();
	}

	public String[] getParameterValues(String name) {
		if(model!=null){
			Object value=model.get(name);
			if(value!=null){
				if(value instanceof String)
					return new String[]{(String)value};
				else if(value.getClass().isArray()){
					String[] values=new String[Array.getLength(value)];
					if(values.length>0){
						for(int i=0;i<values.length;i++)
							values[i]=Array.get(value, i).toString();
					}
					return values;
				}
				else
					return new String[]{value.toString()};
			}
		}
		return request.getParameterValues(name);
	}
	
	
	
	
	// normal delegate methods


	public String getPathInfo() {
		return request.getPathInfo();
	}

	public Object getAttribute(String name) {
		return request.getAttribute(name);
	}

	public Enumeration getAttributeNames() {
		return request.getAttributeNames();
	}

	public String getAuthType() {
		return request.getAuthType();
	}

	public String getCharacterEncoding() {
		return request.getCharacterEncoding();
	}

	public int getContentLength() {
		return request.getContentLength();
	}

	public String getContentType() {
		return request.getContentType();
	}

	public String getContextPath() {
		return request.getContextPath();
	}

	public Cookie[] getCookies() {
		return request.getCookies();
	}

	public long getDateHeader(String name) {
		return request.getDateHeader(name);
	}

	public String getHeader(String name) {
		return request.getHeader(name);
	}

	public Enumeration getHeaderNames() {
		return request.getHeaderNames();
	}

	public Enumeration getHeaders(String name) {
		return request.getHeaders(name);
	}

	public ServletInputStream getInputStream() throws IOException {
		return request.getInputStream();
	}

	public int getIntHeader(String name) {
		return request.getIntHeader(name);
	}

	public String getLocalAddr() {
		return request.getLocalAddr();
	}

	public Locale getLocale() {
		return request.getLocale();
	}

	public Enumeration getLocales() {
		return request.getLocales();
	}

	public String getLocalName() {
		return request.getLocalName();
	}

	public int getLocalPort() {
		return request.getLocalPort();
	}

	public String getMethod() {
		return request.getMethod();
	}

	public String getPathTranslated() {
		return request.getPathTranslated();
	}

	public String getProtocol() {
		return request.getProtocol();
	}

	public String getQueryString() {
		return request.getQueryString();
	}

	public BufferedReader getReader() throws IOException {
		return request.getReader();
	}

	/**
     * @deprecated
     */    
	@SuppressWarnings("deprecation")
	public String getRealPath(String path) {
		return request.getRealPath(path);
	}

	public String getRemoteAddr() {
		return request.getRemoteAddr();
	}

	public String getRemoteHost() {
		return request.getRemoteHost();
	}

	public int getRemotePort() {
		return request.getRemotePort();
	}

	public String getRemoteUser() {
		return request.getRemoteUser();
	}

	public RequestDispatcher getRequestDispatcher(String path) {
		return request.getRequestDispatcher(path);
	}

	public String getRequestedSessionId() {
		return request.getRequestedSessionId();
	}

	public String getRequestURI() {
		return request.getRequestURI();
	}

	public StringBuffer getRequestURL() {
		return request.getRequestURL();
	}

	public String getScheme() {
		return request.getScheme();
	}

	public String getServerName() {
		return request.getServerName();
	}

	public int getServerPort() {
		return request.getServerPort();
	}

	public HttpSession getSession() {
		return request.getSession();
	}

	public HttpSession getSession(boolean create) {
		return request.getSession(create);
	}

	public Principal getUserPrincipal() {
		return request.getUserPrincipal();
	}

	public boolean isRequestedSessionIdFromCookie() {
		return request.isRequestedSessionIdFromCookie();
	}
	/**
     * @deprecated
     */    
	@SuppressWarnings("deprecation")
	public boolean isRequestedSessionIdFromUrl() {
		return request.isRequestedSessionIdFromUrl();
	}

	public boolean isRequestedSessionIdFromURL() {
		return request.isRequestedSessionIdFromURL();
	}

	public boolean isRequestedSessionIdValid() {
		return request.isRequestedSessionIdValid();
	}

	public boolean isSecure() {
		return request.isSecure();
	}

	public boolean isUserInRole(String role) {
		return request.isUserInRole(role);
	}

	public void removeAttribute(String name) {
		request.removeAttribute(name);
	}

	public void setAttribute(String name, Object o) {
		request.setAttribute(name, o);
	}

	public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
		request.setCharacterEncoding(env);
	}
	
	
	

}
